package com.lfy.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.weaver.loadtime.definition.Definition;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class Accpet {
    @Pointcut("@annotation(com.lfy.aop.CheckJWT)")
    public void easyPuct(){

    }

    @Around("easyPuct()")
    public Object checkJWT(ProceedingJoinPoint point){
        log.info("进入aop");
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String jwt = request.getHeader("jwt");
        //伪代码，验证JWT
        if("1".equals(jwt)){
            try {

              return point.proceed();



            } catch (Throwable e) {
                log.error("验证JWT异常，异常:{}，方法：{}，入参：{}，JWT:{}",e.getMessage(),point.getSignature(),point.getArgs(),jwt);
                return "验证失败";
            }
        }else {
            return "jwt错误";
        }



    }

}
